package com.yhp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.common.mapper.BisNurseBagMapper;
import com.system.common.mapper.BisNurseProjectMapper;
import com.system.common.pojo.BisNurseBag;
import com.system.common.pojo.BisNurseBagExample;
import com.system.common.pojo.BisNurseBagExample.Criteria;
import com.system.common.pojo.BisNurseProject;

@Service
public class FrontBisNurseBagServiceImpl implements FrontBisNurseBagService {
	@Autowired
	private BisNurseBagMapper bagMapper;
	@Autowired
	private BisNurseProjectMapper bnpMapper;

	/**
	 * 礼包>>>>>>列表显示所有+综合查询
	 */
	@Override
	public List<BisNurseBag> selectByExample(Map<String, Object> map) {
		String bagId=(String)map.get("bagId");
		String bagName=(String)map.get("bagName");
		String bagTotalPrice1=(String)map.get("bagTotalPrice1");
		String bagTotalPrice2=(String)map.get("bagTotalPrice2");
		BisNurseBagExample example = new BisNurseBagExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(bagId)) {
			criteria.andBagIdLike("%"+bagId+"%");
		}
		if (StringUtils.isNotBlank(bagName)) {
			criteria.andBagNameLike("%"+bagName+"%");
		}
		if (StringUtils.isNotBlank(bagTotalPrice1)&&bagTotalPrice2==null) {
			criteria.andBagTotalPriceGreaterThanOrEqualTo(Double.parseDouble(bagTotalPrice1));
		}
		if (StringUtils.isNotBlank(bagTotalPrice2)&&bagTotalPrice1==null) {
			criteria.andBagTotalPriceLessThanOrEqualTo(Double.parseDouble(bagTotalPrice2));
		}
		if (StringUtils.isNotBlank(bagTotalPrice1)&&StringUtils.isNotBlank(bagTotalPrice2)) {
			criteria.andBagTotalPriceGreaterThanOrEqualTo(Double.parseDouble(bagTotalPrice1));
			criteria.andBagTotalPriceLessThanOrEqualTo(Double.parseDouble(bagTotalPrice2));
			//criteria.andBagTotalPriceBetween(Double.parseDouble(bagTotalPrice1),Double.parseDouble(bagTotalPrice2));
		}
		List<BisNurseBag> selectByExample = this.bagMapper.selectByExample(example);
		List<BisNurseBag> bagList=new ArrayList<>();
		for (BisNurseBag bisNurseBag : selectByExample) {
			if (bisNurseBag.getIsDel().equals("0")&&bisNurseBag.getStatus().equals("1")) {
				List<BisNurseProject> selectByBagId = bnpMapper.selectByBagId(bisNurseBag.getBagId());
				String projectName="";
				for (BisNurseProject bisNurseProject : selectByBagId) {
					projectName=projectName+bisNurseProject.getProjectName()+"+";
				}
				if(StringUtils.isNotBlank(projectName)){
					bisNurseBag.setProjectName(projectName.substring(0, projectName.length()-1));
				}
				bagList.add(bisNurseBag);
			}
		}
		return bagList;
	}

	/**
	 * 礼包>>>>>>详情
	 */
	@Override
	public BisNurseBag selectByPrimaryKey(String bagId) {
		// TODO Auto-generated method stub
		return bagMapper.selectByPrimaryKey(bagId);
	}
}
